/**
@description: demo for reversing a linkedlist 
@function name: reverseLinkedList 
@arguments: pointer of listHead
@author: timlentse(tinglenxan@gmail.com)
@Day: 2014-11-05
*/
#include <stdio.h>
#include <stdlib.h>

struct linkedList {
	int			_Value;
	struct linkedList	* _Next;
};
typedef  struct linkedList linkedList_t;

linkedList_t* reverseLinkList( linkedList_t* listHead )
{
	linkedList_t	* prev = listHead;
	linkedList_t	* next;
	linkedList_t	* tmp = prev->_Next;
	prev->_Next = NULL;

	while ( tmp != NULL )
	{
		next		= tmp->_Next;
		tmp->_Next	= prev;
		prev		= tmp;
		tmp		= next;
	}
	return(prev);
}


/* Function to push a node */
void push( linkedList_t** listHead, int new_data )
{
	/* allocate node */
	linkedList_t* new_node =
		(linkedList_t *) malloc( sizeof(linkedList_t) );

	/* put in the data  */
	new_node->_Value = new_data;

	/* link the old list off the new node */
	new_node->_Next = *listHead;

	/* move the head to point to the new node */
	*listHead = new_node;
}


/* Function to print linked list */
void printList( linkedList_t *head )
{
	linkedList_t *tmp = head;
	while ( tmp != NULL )
	{
		printf( "%d  ", tmp->_Value );
		tmp = tmp->_Next;
	}
}


int main( int argc, char* argv[] )
{  
    linkedList_t *head = NULL;
	push( &head, 20 );
	push( &head, 4 );
	push( &head, 15 );
	push( &head, 85 );

	printList( head );
	linkedList_t *reverseHead = NULL;
	reverseHead = reverseLinkList( head );
	printf( "\n Reversed Linked list \n" );
	printList( reverseHead );
	return(0);
}


